package cn.school.controller.workmgr;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.school.common.IdWorker;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.utils.RedisUtils;
import cn.school.controller.test.FileUploadUtils;
import cn.school.controller.test.Result;
import cn.school.controller.test.ResultUtil;
import cn.school.workmgr.entity.RecruitBanner;
import cn.school.workmgr.entity.RecruitEnroll;
import cn.school.workmgr.entity.RecruitEnrollEntity;
import cn.school.workmgr.service.RecruitBannerService;
import cn.school.workmgr.service.RecruitEnrollService;
import cn.school.workmgr.vo.RecruitBannerVO;
import cn.school.workmgr.vo.RecruitEnrollVO;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "招生banner")
@RequestMapping("/workmgr/enroll")
public class RecruitEnrollController {
    @Resource
    private RecruitEnrollService recruitEnrollService;
    @Resource
    private FileUploadUtils fileUploadUtils;
    @Value("${school.imagePathStartDir}")
    private String filePath;
    @Resource
    private RedisUtils redisUtils;

    @ApiOperation("分页查询")
    @PostMapping("/pageList")
    public CommonResult<CommonPage<RecruitEnroll>> list(@RequestBody RecruitEnrollVO vo) {
        CommonPage<RecruitEnroll> recruitMajorIPage = recruitEnrollService.pageList(vo);
        return CommonResult.success(recruitMajorIPage);
    }

    @ApiOperation("新增登记")
    @PostMapping("/save")
    public CommonResult<Integer> saveRecruitEnroll(@RequestBody RecruitEnrollEntity entity, HttpServletRequest resquest) {
        int num  = recruitEnrollService.saveRecruitEnroll(entity);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 上传多个文件
     *
     * @param file 上传文件集合
     * @return
     */
    @PostMapping(value = "/uploadImage")
    @ApiOperation("上传文件")
    public Result<String> upload(MultipartFile file, HttpServletRequest resquest) throws IOException {
        String token = resquest.getParameter("sourceSystem");
        if(!token.equals("KDH12J4L45JL4L6LL8HGDF9989834DF12")){
            return ResultUtil.error("数据异常");
        }
        return fileUploadUtils.upload(file, filePath);
    }

    /**
     * 获取提交token
     * @return
     */
    @GetMapping(value = "/getToken/{tenantId}")
    @ApiOperation("免登录token")
    public CommonResult<Map<String,Object>> getToken(@PathVariable("tenantId") String tenantId){
        Map<String,Object> map = new HashMap<>();
        IdWorker snowFlake = new IdWorker(30, 30);
        String id = String.valueOf(snowFlake.nextId());
        map.put("tenantId",tenantId);
        map.put("timestamp", id);
        String json = JSON.toJSONString(map);
        String base64Str = Base64.encode(json.getBytes(StandardCharsets.UTF_8));
        redisUtils.set(snowFlake.toString(), base64Str ,60);
        return CommonResult.success(map);
    }
}
